package ding;

import ding.fram.Deeper;
import ding.fram.Finder;
import ding.fram.Group;
import ding.utils.ExcelUtils;

import java.io.*;
import java.util.*;

public class Main {
/*

    private static void read(MapFile<String, String> map) {

        FileTest file = new FileTest("C:\\Users\\dinglei\\Desktop\\部门.txt");

        try {


            BufferedReader reader = new BufferedReader(new FileReader(file));


            String line;

            StringBuilder all = new StringBuilder();

            while ((line = reader.readLine()) != null) {
                all.append(line).append("\n");
            }
            String s = all.toString();

            String[] split = s.split("\n");

            for (String l : split) {
                String[] strings = l.split("\t");

                try {
                    String name = strings[0];
                    String code = strings[1];
                    map.put(code, name);
                } catch (Exception e) {
                    System.err.println(l);
                }

            }


        } catch (IOException e) {
            e.printStackTrace();
        }


    }

    public static void main(String[] args) throws IOException {

        MapFile<String, String> code_name = new HashMap<>();


        read(code_name);

        //尝试处理主表
        ExcelUtils.MappingBuilder builder = new ExcelUtils.MappingBuilder();
        map(builder);

        groupsWrapper group = new groupsWrapper();

        group.add("1个人信息-主表", "2工作信息-主表");

        BufferedWriter writer = new BufferedWriter(new WorkFileWriter(new FileTest("C:\\Users\\dinglei\\Desktop\\无法处理文件.txt")));

        group.setCannotMappingHandler((name, value) -> {
            return null;
        });


        group.setNameValueMakerMap(builder.build());

        List<String> name_idcard = new ArrayList<>();


        group.setKvHandler((objectMap) -> {

            String name = objectMap.get("name");

            String idcard = objectMap.get("idcard");

            String pn = objectMap.remove("__PARENT_FILE_NAME__");

            String ap = objectMap.remove("__ABSOLUTE_FILE_PATH__");

            if (name == null || name.equals("")) {
                try {
                    writer.write(ap + "|名称为空无法处理\n");
                } catch (IOException e) {
                    e.printStackTrace();
                }
                return;
            }

            if (idcard == null || idcard.equals("")) {
                try {
                    writer.write(ap + "|身份证号为空无法处理\n");
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            String one = name + "-" + idcard;

            if (name_idcard.contains(one)) {
                try {
                    writer.write(ap + "|重复的身份证号和名称\n");
                } catch (IOException e) {
                    e.printStackTrace();
                }
                return;
            }

            name_idcard.add(one);


            String code = objectMap.get("org_code");

            if (code != null) {

                String oName = code_name.get(code);

                if (oName == null) {
                    objectMap.remove("org_code");
                } else {
                    objectMap.put("org_name", oName);
                }

            }

            String gender = objectMap.get("gender");

            if (gender == null) {
                objectMap.put("gender", "2");
            } else {
                switch (gender) {
                    case "男":
                        objectMap.put("gender", "0");
                        break;
                    case "女":
                        objectMap.put("gender", "1");
                        break;
                    default:
                        objectMap.put("gender", "2");
                        break;
                }
            }

            StringBuilder names = new StringBuilder("sysid,sysdataid,create_time,remark");

            String id = UUID.randomUUID().toString();

            StringBuilder values = new StringBuilder("\"" + id + "\",\"" + id + "\"" + ",\"2019-11-12 00:00:00\"" + ",\"" + pn + "\"");

            for (String key : objectMap.keySet()) {
                names.append(",").append(key);
                Object value = objectMap.get(key);
                if (value == null) {
                    values.append(",NULL");
                } else {
                    values.append(",\"").append(objectMap.get(key)).append("\"");
                }
            }


            String sql = "INSERT INTO tc_md_personnel (" + names + ") VALUES(" + values + ");\n";

            write(sql);

        });

        Finder finder = new Finder();

        //找到张宇
        //finder.addName("张宇");

        finder.setDefaultName("默认名称");

        finder.addGroup(group);

        finder.setHandleError((f, e) -> {
            try {

                writer.write(f.getAbsolutePath() + "|" + e + "\n");
            } catch (IOException e1) {
                e1.printStackTrace();
            }
        });

        Deeper deeper = new Deeper();

        deeper.setDirHandler(finder);

        deeper.setNameCreater(finder);

        FileTest file = new FileTest("C:\\Users\\dinglei\\Desktop\\格式化后");

        FileTest[] files = file.listFiles();

        if (files != null) {
            for (FileTest f : files) {
                deeper.read(f);
            }
        }


        writer.flush();

        writer.close();

    }

    private static int i = 0;

    private static int j = 999999999 + 1;

    private static BufferedWriter writer;

    private static FileTest root = new FileTest("C:\\Users\\dinglei\\Desktop\\sql");

    private static void write(String sql) {

        if (j > 999999999) {

            if (writer != null) {
                try {
                    writer.flush();
                    writer.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            FileTest file = new FileTest(root, "sql_" + i + ".sql");

            if (!file.exists()) {
                try {
                    file.createNewFile();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            try {
                writer = new BufferedWriter(new WorkFileWriter(file));
            } catch (IOException e) {
                e.printStackTrace();
            }

            i++;
            j = 0;
        }


        try {
            writer.write(sql);
            j++;
        } catch (IOException e) {
            e.printStackTrace();
        }



    }

    private static void map(ExcelUtils.MappingBuilder builder) {

        builder.autoMap("姓名", "name");

        builder.autoMap("身份证号", "idcard");

        builder.autoMap("性别", "gender");

        builder.autoMap("组织机构编码", "org_code");

        builder.autoMap("组织机构名称", "org_name");


*/
/*
        builder.map("最高学历", "zgxl", o -> {

            if (o == null) {
                return null;
            }

            String value = o.getStringCellValue();

            if (value.equals("专科")) {

                return "zk";
            } else {
                return "bk";
            }

        });*//*



*/
/*

        builder.autoMap("性别", "gender");

        builder.autoMap("出生日期", "birthday");

        builder.autoMap("人员编码", "person_code");

        builder.autoMap("公司名称", "company_name");

        builder.autoMap("公司编码", "company_code");

        builder.autoMap("所在部门", "dept_name");

        builder.autoMap("部门编码", "dept_code");

        builder.autoMap("岗位", "jobs");

        builder.autoMap("人员类别", "person_category");

        builder.autoMap("工作状态", "work_status");

        builder.autoMap("个人身份", "personal_status");

        builder.autoMap("任职类型", "post_type");

        builder.autoMap("职务簇", "post_cluster");

        builder.autoMap("职务名称", "post_name");

        builder.autoMap("职务职别", "post_level");

        builder.autoMap("到职日期", "arrival_date");

        builder.autoMap("转岗时间", "transfer_jobs_date");

        builder.autoMap("办公地点", "office_location");

        builder.autoMap("办公电话", "office_phone");

        builder.autoMap("手机号", "mobile_phone");

        builder.autoMap("办公邮箱", "office_mail");

        builder.autoMap("个人邮箱", "personal_mail");

        builder.autoMap("微信", "wechat");

        builder.autoMap("QQ", "qq");

        builder.autoMap("其他通讯方式", "other_contact");

        builder.autoMap("用工形式", "workers_form");

        builder.autoMap("技术职称", "technical_titles");

        builder.autoMap("专业技术职务", "technical_position");

        builder.autoMap("入党团日期", "join_cpcorccyl_date");

        builder.autoMap("档案保管单位", "archive_keep_company");

        builder.autoMap("进入集团日期", "entry_group_date");

        builder.autoMap("进入中铁系统时间", "entry_group_date");

        builder.autoMap("到本单位日期", "entry_group_date");

        builder.autoMap("工龄", "work_years");

        builder.autoMap("从事专业", "devote_major");

        builder.autoMap("享受待遇级别", "entitlement_level");

        builder.autoMap("人员分类", "person_sort");

        builder.autoMap("提职时间", "promotion_date");

        builder.autoMap("进入来源", "entry_sources");

        builder.autoMap("离职日期", "quit_date");

        builder.autoMap("离职原因", "quit_reason");

        builder.autoMap("减员途径", "attrition_way");

        builder.autoMap("薪酬档别", "pay_scale");

        builder.autoMap("薪酬福利停发日期", "pay_stop_date");

        builder.autoMap("是否在岗", "orduty");

        builder.autoMap("是否返聘", "orrehire");

        builder.autoMap("职员标识", "worker_flag");

        builder.autoMap("职员编号", "worker_code");

        builder.autoMap("岗位等级", "jobs_level");

        builder.autoMap("招聘来源", "recruit_source");

        builder.autoMap("是否转正", "orregular");

        builder.autoMap("转正日期", "regular_date");

        builder.autoMap("管理层级", "manage_level");

        builder.autoMap("人事令号", "human_affairs_order");

        builder.autoMap("原人员编码", "old_person_code");

        builder.autoMap("岗位职责", "jobs_duty");

        builder.autoMap("离职令号", "quit_order");

        builder.autoMap("内退待遇", "early_retired_pay");

        builder.autoMap("内退时间", "early_retired_date");

        builder.autoMap("户口性质", "household_nature");

        builder.autoMap("政治面貌", "politic_face");

        builder.autoMap("籍贯", "native_place");

        builder.autoMap("邮政编码", "post_code");

        builder.autoMap("户口所在地", "household_place");

        builder.autoMap("民族", "nation");

        builder.autoMap("年龄", "age");

        builder.autoMap("家庭地址", "family_addr");

        builder.autoMap("家庭电话", "family_phone");

        builder.autoMap("曾用名", "before_used_name");

        builder.autoMap("拼音", "pinyin");

        builder.autoMap("简拼", "short_pinyin");

        builder.autoMap("参加工作日期", "join_work_date");

        builder.autoMap("身高cm", "height");

        builder.autoMap("体重kg", "weight");

        builder.autoMap("婚姻状况", "marital_status");

        builder.autoMap("健康状况", "health_status");

        builder.autoMap("国家", "country");

        builder.autoMap("出生地", "birthplace");

        builder.autoMap("最高学历", "degree");

        builder.autoMap("侨属标识", "qiaoshu_flag");

        builder.autoMap("个人特长", "person_speciality");

        builder.autoMap("特殊人员标识", "special_person_flag");

        builder.autoMap("社保参保地", "social_security_place");

        builder.autoMap("社会保障号", "social_security_num");

        builder.autoMap("医保号", "medical_insurance_num");

        builder.autoMap("养老保险开始缴纳时间", "endowment_insurance_starttime");

        builder.autoMap("视同缴费年限", "treatas_pay_years");

        builder.autoMap("省自治区", "province");

        builder.autoMap("城市地区", "city");

        builder.autoMap("结离婚日期", "marry_or_divorce_date");

        builder.autoMap("兴趣爱好", "hobby");

        builder.autoMap("第一外语", "first_foreign_language");

        builder.autoMap("第一外语级别", "first_foreign_language_level");

        builder.autoMap("计算机水平", "computer_level");

        builder.autoMap("血型", "blood_type");

        builder.autoMap("供养亲属人数", "provide_relatives_num");

        builder.autoMap("是否纳入社会统筹", "oroverall_social_plan");

        builder.autoMap("工人技术等级名称", "worker_technical_level");

        builder.autoMap("预计退休时间", "expect_retire_date");

        builder.autoMap("工伤等级", "industrial_injury_grade");

        builder.autoMap("英文名字", "english_name");
*//*



       */
/* builder.autoMap("最新到岗日期", "latest_board_date");

        builder.autoMap("助记码", "mnemonic_code");

        builder.autoMap("人员顺序号", "personnel_order_number");

        builder.autoMap("归属公司", "belong_company");

        builder.autoMap("是否今年参加评审", "join_review");

        builder.autoMap("创建时间", "create_time");

        builder.autoMap("更新时间", "update_time");*//*



    }

*/

}
